#include "../../include/Configuration.h"
#include "../../include/KeyRecommander/Dictionary.h"
#include "../../include/WebPageSercher/WebPageQuery.h"
#include "../../include/SearchEnginServer/SearchEngineServer.h"
#include "../../include/LRUCahe/CacheManager.h"
#include "../../include/TimerManager/TimerFd.h"

#include <thread>


using std::string;
using std::thread;
using std::cout;
using std::endl;

using namespace SearchEngine;

int main()
{
    map<string,string> configs = Configuration::getInstance()->getcConfigs();

    int threadNum = stoi(configs["threadNum"]);
    int queSize   = stoi(configs["queSize"]);
    string ip     = configs["ip"];
    size_t port   = stoi(configs["port"]);

    //将关键字推荐部分的三个文件内容加载到内存中
    //并用数据结构存起来，三个文件分别是
    //词典库，索引库，idMap
    Dictionary::getInstance();

    //网页库三个文件
    //网页库，网页偏移库，倒排索引库
    WebPageQuery::getWebQueryPtr();

    cout<< "loading server ... "<<endl;


	//预设条件，目的就是为了将缓存管理类CacheManager中的数据成员进行初始化
    CacheManager *p = CacheManager::getInstance();

	//定时器类定时执行更新函数updateCache与updateCache2，更新缓存
	//两份代码逻辑是一样的，就是为了在真正查询前，将缓存跑起来
    TimerFd tf1(2, 3, std::bind(&CacheManager::updateCache, p));
    thread th1(&TimerFd::start, &tf1);

    TimerFd tf2(3, 5, std::bind(&CacheManager::updateCache2, p));
    thread th2(&TimerFd::start, &tf2);
    //运行服务器的代码，进行正在的查询操作
    SearchEngineServer seServer(threadNum,queSize,ip,port);
    seServer.start();
    return 0;











}



